%%%-----------------------------------
%%% @Module  : pt_13
%%% @Author  : kxp
%%% @Created : 2016.08.01
%%% @Description: 13物品信息
%%%-----------------------------------
-module(pt_13).
-export([read/2, write/2,time_to_string/1]).
-include("common.hrl").
-include("proto_13_goods.hrl").

%%
%%客户端 -> 服务端 ----------------------------
%%

%%请求物品信息
read(13000, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_goods_info, Bin),	
	{c2s_goods_info ,Location} = ClientData,
    {ok, [Location]};

%%使用物品
%% read(13001, Bin) ->
%% 	{ClientData, _} = proto_read:unpack(c2s_use_goods, Bin),	
%% 	{c2s_use_goods ,Goods_id, Goods_num} = ClientData,
%%     {ok, [Goods_id, Goods_num]};

%% 开宝箱
read(13002, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_open_treasure_box, Bin),	
	{c2s_open_treasure_box ,Goods_id, MinQualityGTID, Goods_num} = ClientData,
    {ok, [Goods_id, MinQualityGTID, Goods_num]};

%% 装备皮肤
read(13003, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_equip_goods, Bin),	
	{c2s_equip_goods ,Goods_id} = ClientData,
    {ok, [Goods_id]};

%% 卸下皮肤
read(13004, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_demount_equipment, Bin),	
	{c2s_demount_equipment ,Goods_id} = ClientData,
    {ok, [Goods_id]};

%% 13006 进入背包
read(13006, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_enter_backpack, Bin),    
    {c2s_enter_backpack ,PlayerId} = ClientData,
    {ok, [PlayerId]};

%% 13007 合成英雄
read(13007, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_compose_hero, Bin),    
    {c2s_compose_hero, GoodsID, LuckyCharmNum} = ClientData,
    {ok, [GoodsID, LuckyCharmNum]};

%% 13008 获取熔炼列表
read(13008, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_get_smelt_list, Bin),    
    {c2s_get_smelt_list, UID} = ClientData,
    {ok, [UID]};

%% 13009 熔炼物品
read(13009, Bin) ->
	  %%io:format("~n===13009==Bin:~p==~n", [Bin]),
    {ClientData, _} = proto_read:unpack(c2s_smelt_goods, Bin),    
    {c2s_smelt_goods, SmeltID, IsUseProtectCharm, GoodsList} = ClientData,
	  NewGoodsList = [{GTID, Num} || {_, GTID, Num} <- GoodsList],
	  %%io:format("~n===13009===ClientData:~p=======~n", [ClientData]),
    {ok, [SmeltID, IsUseProtectCharm, NewGoodsList]};

%% 13010 宝箱抽奖
read(13010, Bin) ->
	  %%io:format("~n===13010==Bin:~p==~n", [Bin]),
    {ClientData, _} = proto_read:unpack(c2s_box_draw, Bin),    
    {c2s_box_draw, DrawID, Num} = ClientData,
    {ok, [DrawID, Num]};

%% 13011 记牌器
read(13011, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_mem_cards_info, Bin),
    {ok, [ClientData]};

%% 13012 卡包
read(13012, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_card_bag, Bin),
    {c2s_card_bag, CardID, CardPass} = ClientData,
    {ok, [CardID, CardPass]};

%% 13013 首充礼包 
read(13013, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_first_recharge_num, Bin),
    {c2s_first_recharge_num, Uid} = ClientData,
    {ok, [Uid]};

%% 13014 砸金蛋
read(13014, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_hit_egg, Bin),
    {c2s_hit_egg, DrawID, Num} = ClientData,
    {ok, [DrawID, Num]};

read(_Cmd, _R) ->
    ?ERROR_MSG("~s pt_10 reading cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), _Cmd, _R]),
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%
%%返回物品信息
write(13000, [Seq, RecordData]) ->
%% 	io:format("~n===write===13000=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_goods_info, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13000, ServerData)};

%%返回使用物品 
%% write(13001, [Seq, RecordData]) ->
%%     MsgPack = proto_write:pack(s2c_use_goods, RecordData),
%%     ServerData = <<Seq:32, MsgPack/binary>>,
%%     {ok, pt:pack(13001, ServerData)};

%% 开宝箱
write(13002, [Seq, RecordData]) ->
%% 	io:format("~n===write===13002=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_open_treasure_box, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13002, ServerData)};

%% 穿装备
write(13003, [Seq, RecordData]) ->
%% 	io:format("~n===write===13003=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_equip_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13003, ServerData)};

%% 卸装备
write(13004, [Seq, RecordData]) ->
%% 	io:format("~n===write===13004=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_demount_equipment, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13004, ServerData)};

%%返回物品信息
write(13005, [Seq, RecordData]) ->
	%%io:format("~n===write===13005=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_add_new_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13005, ServerData)};

%% 13006 进入背包
write(13006, [Seq, RecordData]) ->
    %%io:format("~n===write===13005=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_enter_backpack, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13006, ServerData)};

%% 13007 合成英雄
write(13007, [Seq, RecordData]) ->
    %%io:format("~n===write===13007=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_compose_hero, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13007, ServerData)};

%% 13008 合成英雄
write(13008, [Seq, RecordData]) ->
    %%io:format("~n===write===13008=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_get_smelt_list, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13008, ServerData)};

%% 13009 合成英雄
write(13009, [Seq, RecordData]) ->
    %%io:format("~n===write===13009=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_smelt_goods, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13009, ServerData)};

%% 13010 宝箱抽奖
write(13010, [Seq, RecordData]) ->
    %%io:format("~n===write===13010=========RecordData:~p===========~n", [RecordData]),
    MsgPack = proto_write:pack(s2c_box_draw, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13010, ServerData)};

%% 13011 记牌器
write(13011, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_mem_cards_info, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13011, ServerData)};

%% 13012 卡包
write(13012, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_card_bag, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13012, ServerData)};

%% 13013 首充人数
write(13013, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_first_recharge_num, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13013, ServerData)};

%% 13014 砸金蛋
write(13014, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_hit_egg, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(13014, ServerData)};

write(Cmd, _R) ->
    ?ERROR_MSG("~s pt_10 writting cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), Cmd, _R]),
    {ok, pt:pack(0, <<>>)}.

time_to_string(Time) ->
	{{_Year, Month, Day},{Hour, M, _S}} = util:seconds_to_localtime(Time),
	Content = io_lib:format("~2..0w月~2..0w日~2..0w:~2..0w",[Month, Day, Hour, M]),
	Content.
	
